//
//  ChangePasswordViewController.m
//  iStore
//
//  Created by Geek on 15/5/5.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "ChangePasswordViewController.h"
#import "RequestEngine.h"
#import "NewLogViewController.h"


#define kPassRefex  @"[1-9]\\d{5}(?!\\d)"

@interface ChangePasswordViewController ()

@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"更改密码";
    _oldPassword.keyboardType = UIKeyboardTypeAlphabet;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//正则表达式的判断
- (BOOL) regexFlagWith:(NSString *)regex withParameter:(NSString *)parameter{
    
    NSPredicate *pass = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    return [pass evaluateWithObject:parameter];
    
}

- (IBAction)changePassword:(UIButton *)sender {
    
    NSString *oldPassword = _oldPassword.text; // 原始密码
    NSString *password = _password.text;//密码
    NSString *rePassword = _rePassword.text;//重复密码
    
    if (oldPassword == nil ||[oldPassword isEqualToString:@""] ||password == nil || [password isEqualToString:@""] || rePassword == nil || [rePassword isEqualToString:@""] ) {
        
        alert(@"内容不能为空，请填写完");
        
        return;
    }
    if (![password isEqualToString:rePassword]) {
        alert(@"两次输入的密码不同，请重新输入");
        return;
    }
    if ([oldPassword isEqualToString:password]) {
        alert(@"与原密码相同，请重新输入");
        return;
    }
    
    //判断是否为6个数字
    if(_password.text.length < 6 || _password.text.length >18){
        
        alert(@"您输入的密码不符合规范，请重新输入");
        return;
    }

    NSString *userID = [[NSUserDefaults standardUserDefaults] objectForKey:kUserId];
    NSString *platformType = [[NSUserDefaults standardUserDefaults]objectForKey:KplatformType];
    NSDictionary * changeDict = @{@"userID":userID,@"newPassw":password,@"oldPassw":oldPassword,@"platformType":platformType};
    [self activityStart];
    [RequestEngine changePassWordWithDic: changeDict
                              completed:^(NSString *errorcode, NSDictionary *resultDict) {
                                  
                                  [self activityStop];
                      //            NSLog(@"<<<<<<<<<<<%@",resultDict);
                                  
                                  if ([errorcode isEqualToString:@"0"]) {
                                      
                                      
                                      alert(@"修改密码成功");
                                  
                                  NewLogViewController * newLog = [[NewLogViewController alloc]init];
                                  [self.navigationController pushViewController:newLog animated:YES];
                                }
                                else if ([errorcode isEqualToString:@"1"]){
                                      NSString *result = [NSString stringWithFormat:@"%@",resultDict];
                                      alert(result);
                                  }
                                  else
                                      alert(@"请检查网络状况");
                                       
                            }];
    
}
@end
